package com.mingtu.guards.activity;

import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.mingtu.common.callback.StringDialogCallback;
import com.hjq.gson.factory.GsonFactory;
import com.mingtu.common.utils.ActivityUtil;
import com.mingtu.common.utils.Constant;
import com.mingtu.guards.R;
import com.mingtu.guards.base.MyBaseActivity;
import com.mingtu.guards.bean.TaskDetailsBean;
import com.mingtu.guards.utils.MyConstant;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class TaskDetails2Activity extends MyBaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.layout_status)
    LinearLayout layoutStatus;
    @BindView(R.id.tv_complete)
    TextView tvComplete;
    @BindView(R.id.tv_task_type)
    TextView tvTaskType;
    @BindView(R.id.tv_time_start)
    TextView tvTimeStart;
    @BindView(R.id.tv_time_end)
    TextView tvTimeEnd;
    @BindView(R.id.tv_complete_time)
    TextView tvCompleteTime;
    @BindView(R.id.button)
    Button button;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.layout_bottom)
    RelativeLayout layoutBottom;
    @BindView(R.id.layout_complete)
    LinearLayout layoutComplete;
    private String taskId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_task_details2;
    }

    @Override
    protected void initView() {
        setModuleTitle("任务详情");
        setTitleBarBackground(getResources().getDrawable(R.drawable.titlebar_line_bottom));
    }

    @Override
    protected void initData() {
        taskId = getIntent().getStringExtra("taskId");
        if (!StringUtils.isEmpty(taskId)) {
            getTaskDetails();
        } else {
            ToastUtils.showLong(getResources().getString(R.string.data_error));
        }
    }

    @Override
    protected void getData() {

    }

//    @Override
//    protected void onClickedTopLeftButtton(View view) {
//        setResult(200);
//        super.onClickedTopLeftButtton(view);
//    }
//
//    @Override
//    protected void onKeyDownEvnet() {
//        setResult(200);
//        super.onKeyDownEvnet();
//    }

    @OnClick(R.id.button)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.button:
                finishTask();
                break;
        }
    }

    /**
     * 完成任务操作
     */
    private void finishTask() {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("id", taskId);
        String str = GsonUtils.toJson(hashMap);
        OkGo.<String>post(MyConstant.POST_FINISH_TASK__TEM + "/" + taskId)
                .tag(this)
                .upJson(str)
                .execute(new StringDialogCallback(this) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {
                            JSONObject jsonObject = JSON.parseObject(result);
                            String msg = jsonObject.getString("msg");
                            if (msg.equals(Constant.RESULT_SUCCESS)) {
                                ToastUtils.showLong("任务完成！");
                                layoutStatus.setVisibility(View.GONE);
                                layoutBottom.setVisibility(View.GONE);
                                tvComplete.setVisibility(View.VISIBLE);
                                setResult(200);
                                ActivityUtil.removeActivity(TaskDetails2Activity.this);
                            }

                        }
                    }
                });
    }

    /**
     * 任务详情
     */
    private void getTaskDetails() {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("id", taskId);
        String str = GsonUtils.toJson(hashMap);
        OkGo.<String>post(MyConstant.POST_SEND_TASK_DETAILS + "/" + taskId)
                .tag(this)
                .upJson(str)
                .execute(new StringDialogCallback(this) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {
                            Gson gson = GsonFactory.getSingletonGson();
                            TaskDetailsBean taskDetailsBean = new TaskDetailsBean();
                            taskDetailsBean = gson.fromJson(result, TaskDetailsBean.class);
                            TaskDetailsBean.AssignmentBean assignmentBean = taskDetailsBean.getAssignment();
                            List<TaskDetailsBean.AssignmentBean.AssignmentUserEntityListBean> listBeans = assignmentBean.getAssignmentUserEntityList(); //接收人员
                            String taskName = assignmentBean.getName();
                            String dealineTime = assignmentBean.getFinishTime();
                            String createTime = assignmentBean.getCreateTime();
                            String content = assignmentBean.getMemo();
                            String isFinish = assignmentBean.getIsFinish();
//                            int status = assignmentBean.getStatus(); // 0: 未完成   1：已完成

                            if (isFinish.equals("0")) {
                                layoutStatus.setVisibility(View.VISIBLE);
                                layoutBottom.setVisibility(View.VISIBLE);
                            }

                            if (isFinish.equals("1")) {
                                tvComplete.setVisibility(View.VISIBLE);
                                layoutComplete.setVisibility(View.VISIBLE);
                                if (listBeans != null) {
                                    if (listBeans.size() > 0) {
                                        for (int i = 0; i < listBeans.size(); i++) {
                                            int statusItem = listBeans.get(i).getStatus();
                                            if (statusItem == 1) {
                                                String completeTime = listBeans.get(i).getCompleteTime();
                                                tvCompleteTime.setText(completeTime + "");
                                            }
                                        }
                                    }
                                }
                            }


                            if (!StringUtils.isEmpty(taskName)) {
                                tvTitle.setText(taskName);
                            }

                            if (!StringUtils.isEmpty(createTime)) {
                                tvTimeStart.setText(createTime);
                            }

                            if (!StringUtils.isEmpty(dealineTime)) {
                                tvTimeEnd.setText(dealineTime);
                            }

                            if (!StringUtils.isEmpty(content)) {
                                tvContent.setText(content);
                            }


                        }
                    }
                });
    }

}